package com.myblog.config;

import com.myblog.pojo.CustomDataSource;
import org.I0Itec.zkclient.ZkClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;

@Configuration
public class DataSourceConfig {

    @Bean
    public DataSource dataSource() {

        ZkClient zkClient = new ZkClient("127.0.0.1:2181");
        String path = "/datasource";
        String datasource = zkClient.readData(path).toString();

        String username = datasource.split("-")[0];
        String password = datasource.split("-")[1];
        String url = datasource.split("-")[2];

        System.out.println("获取初始化用户名:"+username+" 密码:"+password+" 数据库连接:"+url);

        CustomDataSource dataSource = new CustomDataSource ();
        dataSource.setDriverClassName("com.mysql.jdbc.Driver");
        dataSource.setUsername(username);
        dataSource.setPassword(password);
        dataSource.setUrl(url);
        return dataSource;
    }

}
